using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Phone.Tasks;


namespace AdRotatorXNA
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    internal class AdRotatorDefaultHouseAd : SLXnaComponentSystem.DrawableGameComponent
    {
        private static AdRotatorDefaultHouseAd instance;

        private static Texture2D DefaultTexture;

        public delegate void OnAdClicked(object sender, EventArgs e);

        public event OnAdClicked AdClicked;
        
        private Vector2 adPosition = Vector2.Zero;

        private Rectangle BannerRect = Rectangle.Empty;

        private bool Initialised = false;

        private AdRotatorDefaultHouseAd(SpriteBatch spriteBatch)
         //   : base(game)
        {
            // TODO: Construct any child components here
        }


        public static AdRotatorDefaultHouseAd Current
        {
            get
            {
                if (AdRotatorXNAComponent.spriteBatch == null)
                {
                    return null;
                }
                if (instance == null)
                {
                    instance = new AdRotatorDefaultHouseAd(AdRotatorXNAComponent.spriteBatch);
                }

                return instance;
            }
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            Initialised = true;
            base.Initialize();
        }

        public static void Initialize(SpriteBatch _spriteBatch, Texture2D defaultTexture)
        {
            if (defaultTexture == null)
            {
                defaultTexture = new Texture2D(_spriteBatch.GraphicsDevice, AdRotatorXNAComponent.Current.AdWidth, AdRotatorXNAComponent.Current.AdHeight);
                defaultTexture.SetData<Color>(new Color[1] { Color.Gray });
            }
            DefaultTexture = defaultTexture;
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(TimeSpan elapsedTime, TimeSpan totalTime)
        {
            // TODO: Add your update code here
            var TouchState = TouchPanel.GetState();
            foreach (var item in TouchState)
            {
                if (BannerRect.Contains(new Point((int)item.Position.X - 80, (int)item.Position.Y)))
                {
                    if (AdClicked != null)
                    {
                        AdClicked(null, new EventArgs());
                    }
                }
            }

            base.Update(elapsedTime, totalTime);
        }

        public void UpdateAdPosition(Vector2 newposition)
        {
            adPosition = newposition;
            if (Initialised) UpdateBanner();
        }

        private void UpdateBanner()
        {
            BannerRect = new Rectangle((int)adPosition.X, (int)adPosition.Y, GraphicsDevice.Viewport.TitleSafeArea.Width - 2, 80);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(TimeSpan elapsedTime, TimeSpan totalTime)
        {
            var spriteBatch = AdRotatorXNAComponent.spriteBatch;
            // TODO: Add your drawing code here
            spriteBatch.Begin();
            spriteBatch.Draw(DefaultTexture, BannerRect, Color.Gray);
            spriteBatch.End();


            base.Draw( elapsedTime,  totalTime);
        }
    }
}
